package DiffieHellman;

import java.math.BigInteger;

public class DHTest {

    private static class DHTestData {
        private final BigInteger xa;
        private final BigInteger xb;
        private final BigInteger ya;
        private final BigInteger yb;
        private final BigInteger K;

        private DHTestData(String xa, String xb, String ya, String yb, String k) {
            this.xa = new BigInteger(xa);
            this.xb = new BigInteger(xb);
            this.ya = new BigInteger(ya);
            this.yb = new BigInteger(yb);
            K = new BigInteger(k);
        }


        public BigInteger getXa() {
            return xa;
        }

        public BigInteger getXb() {
            return xb;
        }

        public BigInteger getYa() {
            return ya;
        }

        public BigInteger getYb() {
            return yb;
        }

        public BigInteger getK() {
            return K;
        }
    }

    public static void main(String[] args) {
        primaryTest();
    }

    private static void primaryTest() {
        BigInteger bigPrime = new BigInteger("2036374273689296142034438587899");
        BigInteger originRoot = new BigInteger("3034683");
        DHTestData[] datas = new DHTestData[]{
                new DHTestData("1153573475466882726858487539880", "56382819013726399044626254683",
                        "1036819405652475734188440387988", "1269973725853698322054741168192",
                        "121376465268231698585106305048"),
                new DHTestData("630142561702485290337074650735", "900816054281603185686186252997",
                        "1287941963023882622329829046322", "1576155571936942573191091617229",
                        "1424400100205334869359880342600"),
                new DHTestData("1594347058928137744806136266747", "1004312614212178016681323664138",
                        "1300657119048987593787522258320", "970288989254472279613928767521",
                        "96511291108447467514916814556")
        };
        boolean flag = true;
        for (DHTestData data : datas) {
            DHexchange dHexchange = new DHexchange(bigPrime, originRoot);
            boolean res = dHexchange.DH(data.getXa(), data.getXb());
            if (!res || !data.getYa().equals(dHexchange.pka) || !data.getYb().equals(dHexchange.pkb)
                    || !data.getK().equals(dHexchange.keya)) {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("DHTest.primaryTest：测试通过");
        } else {
            System.out.println("DHTest.primaryTest：测试不通过");
        }

    }

}
